/*
	This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package net.sf.odinms.client.messages.commands;

import java.rmi.RemoteException;

import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.messages.Command;
import net.sf.odinms.client.messages.CommandDefinition;
import net.sf.odinms.client.messages.CommandProcessor;
import net.sf.odinms.client.messages.IllegalCommandSyntaxException;
import net.sf.odinms.client.messages.MessageCallback;
import net.sf.odinms.net.ExternalCodeTableGetter;
import net.sf.odinms.net.PacketProcessor;
import net.sf.odinms.net.RecvPacketOpcode;
import net.sf.odinms.net.SendPacketOpcode;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.scripting.map.MapScriptManager;
import net.sf.odinms.scripting.portal.PortalScriptManager;
import net.sf.odinms.scripting.reactor.ReactorScriptManager;
import net.sf.odinms.server.life.MapleMonsterInformationProvider;

public class ReloadingCommands implements Command {

    private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ReloadingCommands.class);

    @Override
    public void execute(MapleClient c, MessageCallback mc, String[] splitted) throws Exception, IllegalCommandSyntaxException {
        ChannelServer cserv = c.getChannelServer();
        if (splitted[0].equals("!reloadguilds")) {
            try {
                mc.dropMessage("Attempting to reload all guilds... this may take a while...");
                cserv.getWorldInterface().clearGuilds();
                mc.dropMessage("Guilds reloaded.");
            } catch (RemoteException re) {
                mc.dropMessage("RemoteException occurred while attempting to reload guilds.");
                log.error("RemoteException occurred while attempting to reload guilds.", re);
            }
        } else if (splitted[0].equals("!重新获取包头")) {
            try {
                ExternalCodeTableGetter.服务端包头获取(SendPacketOpcode.getDefaultProperties(), SendPacketOpcode.values());
                ExternalCodeTableGetter.客户端包头获取(RecvPacketOpcode.getDefaultProperties(), RecvPacketOpcode.values());
            } catch (Exception e) {
                log.error("包头重新读取失败", e);
            }
            PacketProcessor.getProcessor(PacketProcessor.Mode.CHANNELSERVER).reset(PacketProcessor.Mode.CHANNELSERVER);
            PacketProcessor.getProcessor(PacketProcessor.Mode.CHANNELSERVER).reset(PacketProcessor.Mode.CHANNELSERVER);
            mc.dropMessage("包头重新读取成功");
        } else if (splitted[0].equals("!重载传送点脚本")) {
            PortalScriptManager.getInstance().clearScripts();
            mc.dropMessage("传送点重载完成.");
        } else if (splitted[0].equals("!重载掉物")) {
            MapleMonsterInformationProvider.getInstance().clearDrops();
            mc.dropMessage("掉物重载完成.");
        } else if (splitted[0].equals("!重载触发器脚本")) {
            ReactorScriptManager.getInstance().clearDrops();
            mc.dropMessage("触发器重载完成");
        } else if (splitted[0].equals("!重载活动脚本")) {
            for (ChannelServer instance : ChannelServer.getAllInstances()) {
                if (instance != null) {
                    instance.reloadEvents();
                }
            }
            mc.dropMessage("活动脚本重载完成");
        } else if (splitted[0].equals("!重载命令")) {
            CommandProcessor.getInstance().reloadCommands();
            mc.dropMessage("命令重载完成");
        } else if (splitted[0].equals("!重载地图脚本")) {
            MapScriptManager.getInstance().clearScripts();
            mc.dropMessage("地图脚本重载完成");
        }
    }

    @Override
    public CommandDefinition[] getDefinition() {
        return new CommandDefinition[]{
            new CommandDefinition("重新获取包头", "", "", 0),
                    new CommandDefinition("reloadguilds", "", "", 50),
                    new CommandDefinition("reloadops", "", "", 50),
                    new CommandDefinition("reloadportals", "", "", 50),
                    new CommandDefinition("reloaddrops", "", "", 50),
                    new CommandDefinition("reloadreactors", "", "", 50),
                    new CommandDefinition("reloadshops", "", "", 50),
                    new CommandDefinition("reloadevents", "", "", 50),
                    new CommandDefinition("reloadcommands", "", "", 50)
                };
    }
}